﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef Texture2D_h__
#define Texture2D_h__
#include "../base/Common.h"
#include "../utility/MacroTool.h"
#include "../base/Rect.h"
#include "../utility/SmartPtrDef.h"

namespace SkyEngine2d
{

	class TextureCache;

	using  D3dShaderResourceViewComPtr = Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>;
	using  D3dResourceComPtr = Microsoft::WRL::ComPtr<ID3D11Resource>;

	/*!
	 \brief 2D纹理
	 \details
	*/
	class SGE_API Texture2D
	{

	public:
		//! 创建一个纹理 根据图片的名字
		/*!
		图片支持JPG、PNG ，当该文件之前已经创建过纹理 这个 直接从 纹理缓存中获取纹理
		\param const std::wstring & file_name 图片的名字
		\return Texture2DSharedPtr 纹理指针
		*/
		static Texture2DSharedPtr create(const std::wstring& file_name);

		//! 析构器
		~Texture2D();

		//! 获取纹理的尺寸
		/*!
		\return Size
		\sa getContentRect()
		*/
		const Size& getContentSize()const;

		//! 获取 ID3D11ShaderResourceView 对象
		/*!
		\return D3dShaderResourceViewComPtr
		\sa getD3dResourceComPtr()
		*/
		D3dShaderResourceViewComPtr getD3dShaderResourceView();

		//! 获取 ID3D11Resource 对象
		/*!
		\return D3dResourceComPtr
		\sa getD3dShaderResourceView()
		*/
		D3dResourceComPtr getD3dResourceComPtr();

	protected:

		//! 无参数构造器
		Texture2D();

		//! 通过图片文件初始化纹理 
		/*!
		\param const std::wstring & file_name 图片名字
		\return bool 初始化是否成功
		*/
		bool initWithFile(const std::wstring& file_name);

	private:
		Size  m_contentSize;	/*!< 纹理的Size. */
		D3dShaderResourceViewComPtr m_textureView;
		D3dResourceComPtr m_resource;

		FRIEND_SMART_PTR(Texture2D);
		friend class TextureCache;
	};

}

#endif // Texture2D_h__
